package com.trkj.usercenter;

import android.content.res.AssetManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.trkj.base.BaseActivity;
import com.trkj.taoruiyun.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Created by LiZanhong on 2015/06/19.
 */
public class ApiActivity extends BaseActivity{
    public static final String ACTION = "com.trkj.usercenter.ApiActivity";

    @ViewInject(R.id.list)
    private ListView listView;

    private JSONArray apis;

    class ApiAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return apis.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if(convertView == null){
                convertView = getLayoutInflater().inflate(R.layout.activity_api_item, null);
                viewHolder = new ViewHolder();
                viewHolder.name = (TextView) convertView.findViewById(R.id.api_item_name);
                viewHolder.url = (TextView) convertView.findViewById(R.id.api_item_url);
                viewHolder.desc = (TextView) convertView.findViewById(R.id.api_item_desc);
                viewHolder.method = (TextView) convertView.findViewById(R.id.api_item_method);
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder)convertView.getTag();
            }

            viewHolder.name.setText(apis.getJSONObject(position).getString("name"));
            viewHolder.url.setText(apis.getJSONObject(position).getString("url"));
            String desc = apis.getJSONObject(position).getString("description");
            if(TextUtils.isEmpty(desc)){
                viewHolder.desc.setVisibility(View.GONE);
            }else {
                viewHolder.desc.setVisibility(View.VISIBLE);
                viewHolder.desc.setText(desc);
            }
            viewHolder.method.setText(apis.getJSONObject(position).getString("method"));

            return convertView;
        }

        class ViewHolder{
            TextView name;
            TextView url;
            TextView desc;
            TextView method;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_common);

        ViewUtils.inject(this);
        convertToJSONArray(readJSON());
        listView.setAdapter(new ApiAdapter());
    }

    /**
     * 读取api.json
     * @return
     */
    protected String readJSON(){
        try {
            AssetManager assetManager = this.getAssets();

            InputStream is = assetManager.open("api.json");
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = br.readLine();
            String all = "";
            while(line != null){
                all += line;
                line = br.readLine();
            }
            br.close();
            is.close();
            //assetManager.close();
            return all;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将json字符串转换成json对象数组
     * @param jsonString
     */
    protected void convertToJSONArray(String jsonString){
        apis = JSONObject.parseObject(jsonString).getJSONArray("requests");
    }

    @Override
    protected String getCustomerTitle() {
        return "API数据接口";
    }
}
